<?php

namespace app\index\controller;

use data\service\TimeLineService;
use think\Controller;
use think\Request;

use data\service\LabelService;
use data\service\ArticleService;
use data\model\Article as ArticleModel;

class Article extends BaseController
{
    /**
     * 前台文章列表
     * @desc
     * @return mixed
     * @author 16
     * @date 2018/2/24
     */
    public function index(Request $request,$type,$label=null,$key=null){
        $this->assign('s',$key);
        $this->assign('labelId',$label);

        //设置对应分类导航
        $this->putLabelByType($type);

        //获取推荐文章
        $this->putHotAericle($type);

        //随便看看
        $this->putArticleLimit(10);

        //获取相应的文章
        $map['type_id'] = $type;
        $label && $map[] = ['label_id','=',$label];
        $key && $map[] = ['title','like','%'.$key.'%'];
        $articles = ArticleService::getArticleByMap($map,'id,label_id,title,desc,cover,create_time,author,brow_volume');
        $this->assign('articles',$articles);

        return $this->fetch('home/technology');
    }

    /**
     * 时间轴
     * @desc
     * @author 16
     * @date 2018/2/24
     */
    public function timeLine(Request $request,$type){
        //获取相应的内容
        $contents = TimeLineService::getContent($type);
        $this->assign('contents',$contents);
        $this->assign('select_id',$type);
        return $this->fetch('home/timeline');
    }

    /**
     * 文章详情
     * @desc
     * @param $article_id
     * @author 16
     * @date 2018/2/24
     */
    public function detail($article_id){
        $article = ArticleModel::get($article_id);
        $this->assign('article',$article);

        //设置对应分类导航
        $this->putLabelByType($article->type_id);

        //获取推荐文章
        $this->putHotAericle($article->type_id);

        //随便看看
        $this->putArticleLimit(10);

        return $this->fetch('article/detail');
    }

    /**
     * 输出不同分类的标签
     * @desc
     * @param $typeId
     * @author 16
     * @date 2018/2/25
     */
    private function putLabelByType($typeId){
        $labels =  LabelService::getLabelByType($typeId);
        $this->assign('labels',$labels);
    }

    /**
     * 输出不同分类的置顶文章
     * @desc
     * @param $typeId
     * @author 16
     * @date 2018/2/25
     */
    private function putHotAericle($typeId){
        $hots = new ArticleService();
        $this->assign('hots',$hots->getHotAericle($typeId));
    }


    /**
     * 随机输出n条文章
     * @desc
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author 16
     * @date 2018/2/25
     */
    private function putArticleLimit($limit){
        $randArts = ArticleService::getArticleLimit($limit,'id,title');
        $this->assign('randArts',$randArts);
    }

}
